package com.mvvmhabit.org

import android.app.Activity
import android.app.Application
import android.os.Bundle
import com.mvvmhabit.org.base.AppManager
import com.mvvmhabit.org.utils.Utils
import java.util.*

/**
 * @Date: 2020/11/17 11:32
 * @Author:小妖
 * @Description:
 */
open class BaseApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        setApplication(this)
    }

    companion object {
        //初始化工具类
        val list = Stack<Activity>()

        private var mInstance: BaseApplication? = null

        /**
         * 获得当前app运行的AppContext
         */
        fun getInstance(): BaseApplication? {
            return mInstance
        }

        fun setApplication(application: BaseApplication) {
            mInstance = application
            //初始化工具类
            Utils.init(application)
            application.registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
                override fun onActivityPaused(activity: Activity) {
                }

                override fun onActivityStarted(activity: Activity) {
                }

                override fun onActivityDestroyed(activity: Activity) {
                    AppManager.getAppManager()?.removeActivity(activity)
                }

                override fun onActivitySaveInstanceState(activity: Activity, bundle: Bundle) {
                }

                override fun onActivityStopped(activity: Activity) {
                }

                override fun onActivityCreated(activity: Activity, bundle: Bundle?) {
                    AppManager.getAppManager()?.addActivity(activity)
                }

                override fun onActivityResumed(activity: Activity) {
                }
            })
        }
    }
}